DRY (Don’t Repeat Yourself) — принцип, согласно которому каждый фрагмент знания должен существовать в системе только в одном месте. Никакого копипаста и дублирующей логики.
Проблема: дублирование валидации
// Пример плохого кода if (user.Age < 18) throw new Exception("User must be at least 18");
...
if (user.Age < 18) return BadRequest("User must be at least 18");
При изменении правила — нужно помнить обновить его везде. Ловушка копипаста.
Решение: вынос логики в общее правило
public static class ValidationRules { public static bool IsAdult(User user) => user.Age >= 18; }
Теперь в коде
if (!ValidationRules.IsAdult(user)) throw new Exception("User must be at least 18");
// И в другом месте: if (!ValidationRules.IsAdult(user)) return BadRequest("User must be at least 18");
Альтернатива: использование FluentValidation или DataAnnotations
public class User { [Range(18, int.MaxValue, ErrorMessage = "User must be at least 18")] public int Age { get; set; } }
Где чаще всего нарушают DRY в .NET:
• Повторяющиеся SQL-запросы и фильтры • Повторение одинаковых exception'ов, логов, сообщений • Дублирование конфигурации • UI-формы и компоненты
DRY (Don’t Repeat Yourself) — принцип, согласно которому каждый фрагмент знания должен существовать в системе только в одном месте. Никакого копипаста и дублирующей логики.
Проблема: дублирование валидации
// Пример плохого кода if (user.Age < 18) throw new Exception("User must be at least 18");
...
if (user.Age < 18) return BadRequest("User must be at least 18");
При изменении правила — нужно помнить обновить его везде. Ловушка копипаста.
Решение: вынос логики в общее правило
public static class ValidationRules { public static bool IsAdult(User user) => user.Age >= 18; }
Теперь в коде
if (!ValidationRules.IsAdult(user)) throw new Exception("User must be at least 18");
// И в другом месте: if (!ValidationRules.IsAdult(user)) return BadRequest("User must be at least 18");
Альтернатива: использование FluentValidation или DataAnnotations
public class User { [Range(18, int.MaxValue, ErrorMessage = "User must be at least 18")] public int Age { get; set; } }
Где чаще всего нарушают DRY в .NET:
• Повторяющиеся SQL-запросы и фильтры • Повторение одинаковых exception'ов, логов, сообщений • Дублирование конфигурации • UI-формы и компоненты
Telegram is an aspiring new messaging app that’s taking the world by storm. The app is free, fast, and claims to be one of the safest messengers around. It allows people to connect easily, without any boundaries.You can use channels on Telegram, which are similar to Facebook pages. If you’re wondering how to find channels on Telegram, you’re in the right place. Keep reading and you’ll find out how. Also, you’ll learn more about channels, creating channels yourself, and the difference between private and public Telegram channels.
Telegram announces Search Filters
With the help of the Search Filters option, users can now filter search results by type. They can do that by using the new tabs: Media, Links, Files and others. Searches can be done based on the particular time period like by typing in the date or even “Yesterday”. If users type in the name of a person, group, channel or bot, an extra filter will be applied to the searches.